;;; joy-mode.el --- major mode for editing Joy source.
;;; Copyright © 2016 Eric Bavier <bavier@member.fsf.org>
;;;
;;; Joy is free software; you can redistribute it and/or modify it under
;;; the terms of the GNU General Public License as published by the Free
;;; Software Foundation; either version 3 of the License, or (at your
;;; option) any later version.
;;;
;;; Joy is distributed in the hope that it will be useful, but WITHOUT
;;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;;; or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
;;; License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with Joy.  If not, see <http://www.gnu.org/licenses/>.
;;; Copyright © 2016 Eric Bavier <bavier@member.fsf.org>


;;; Commentary:

;; Provides syntax highlighting for the small number of primitive
;; operators in Joy, as well as for the fundamental datatypes

;;; Code:

(setq joy-keywords '("DEFINE" "PUBLIC" "PRIVATE" "LIBRA" "HIDE" "END" "IN"))
(setq joy-primitives '("unstack" "display" "include" "logical" "integer"
		       "string" "choice" "uncons" "infra" "stack" "putch"
		       "swap" "cons" "char" "list" "dup" "pop"))

(setq joy-keywords-regexp (regexp-opt joy-keywords 'words))
(setq joy-primitives-regexp (regexp-opt joy-primitives 'words))

(setq joy-font-lock-keywords
      `("==" ";"
	("\\(\\.\\)[^[:digit:]]" 1)
	("\\([[:graph:]]+\\)[[:blank:]]*==" 1 font-lock-function-name-face)
	("'\\\\?[[:alnum:]]\\([[:digit:]][[:digit:]]\\)?" . font-lock-type-face)
	(,joy-keywords-regexp . font-lock-keyword-face)
	(,joy-primitives-regexp . font-lock-builtin-face)))

(defvar joy-syntax-table nil "Syntax table for `joy-mode'.")
(setq joy-syntax-table
      (let ((synTable (make-syntax-table)))
	;; bash style comment: "# ..."
	(modify-syntax-entry ?# "< b" synTable)
	(modify-syntax-entry ?\n "> b" synTable)

	;; Mathematic style comment: "(* ... *)"
	(modify-syntax-entry ?\( ". 1" synTable)
	(modify-syntax-entry ?\) ". 4" synTable)
	(modify-syntax-entry ?* ". 23" synTable)

	synTable))

;;;###autoload
(define-derived-mode joy-mode fundamental-mode
  "Joy mode"
  "Major mode for editing the purely functional,
  concatenative programming language Joy."
  :syntax-table joy-syntax-table

  (setq font-lock-defaults '(joy-font-lock-keywords))
  (setq mode-name "joy"))

;; clear memory. no longer needed
(setq joy-keywords nil)
(setq joy-primitives nil)
(setq joy-keywords-regexp nil)
(setq joy-primitives-regexp nil)

;; add the mode to the `features' list
(provide 'joy-mode)

;; Local Variables:
;; coding: utf-8
;; End:

;;; joy-mode.el ends here
